﻿package fwk.panelInfos {	

	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import fwk.interfaces.IPanelErrors;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	public class ErrorPanel extends MovieClip implements IPanelErrors{
		
		public var _mc:MovieClip;
		
		public var _Main:MovieClip;
		public var _xmlErrors:XML;
		
		// getter, setter
		public function set Main(val:MovieClip)
		{
			_Main = val;
		}
		public function set xmlErrors(val:XML)
		{
			_xmlErrors = val;
		}
		public function get xmlErrors():XML
		{
			return _xmlErrors;
		}
		public function get mc():MovieClip
		{
			return _mc;
		}
		//-----------------------------------------------------------------------
		function ErrorPanel(mc:MovieClip = null){
			if(mc != null) this._mc = mc;
			else this._mc = this;
			
			
			_mc.clip.btConfirm.buttonMode = true;
			_mc.clip.btClose.buttonMode = true;
			
			initRollOver(_mc.clip.btConfirm);
			initRollOver(_mc.clip.btClose);
			
			_mc.clip.btClose.addEventListener(MouseEvent.MOUSE_DOWN, onBtClose);
			_mc.clip.btConfirm.addEventListener(MouseEvent.MOUSE_DOWN, onBtClose);
		}
		//----------------------------------------------------------
		public function init(){

		}
		private function initRollOver(clip:Object){
			clip.addEventListener(MouseEvent.ROLL_OVER, onRollOverClip);
			clip.addEventListener(MouseEvent.ROLL_OUT, onRollOutClip);
		}
		
		private function onRollOverClip(evt:MouseEvent){
			(evt.currentTarget as Object).gotoAndPlay("over");
		}
		//----------------------------------------------------------
		private function onRollOutClip(evt:MouseEvent){
			(evt.currentTarget as Object).gotoAndPlay("out");
		}
		//----------------------------------------------------------
		public function display(obj:Object){
			_mc.x = _Main.stage.stageWidth/2 - _mc.clip.width/2;
			_mc.y = _Main.stage.stageHeight/2 - _mc.clip.height/2;
			
			
			_mc.clip.btConfirm.buttonMode = true;
			_mc.clip.btClose.buttonMode = true;
			
			if(obj as Number){
				trace("display Errors " + _xmlErrors.child("error" + obj)[0].child("text")[0]);
				_Main.initTextFromNode(_mc.clip.description, _xmlErrors.child("error" + obj)[0].child("description")[0]);
				_Main.initTextFromNode(_mc.clip.titre, _xmlErrors.child("error" + obj)[0].child("titre")[0]);
				_mc.gotoAndPlay("display");
				_Main.stage.addChild(_mc);
			}
			
			if(obj as String){
				trace("display Errors " + obj);
				_mc.gotoAndPlay("display");
				_Main.stage.addChild(_mc);
				_Main.initTextFromString(_mc.clip.description, obj, _xmlErrors.child("errorDefault").child("description")[0].attribute("style")[0]);
			}
			
			initKeyListeners();

		}
		//----------------------------------------------------------
		public function addText(obj:Object){
			trace("addText " + obj);
			
			if(obj as Number){
				_mc.clip.description.tTexte.text += _xmlErrors.child("error" + obj)[0].child("description")[0];
			}
			
			if(obj as String){
				_mc.clip.description.tTexte.text += obj;
			}
		}
		//----------------------------------------------------------
		public function onBtClose(evt:MouseEvent){
			startClose();
		}
		//----------------------------------------------------------
		public function startClose() {
			removeKeyListeners();
			_mc.gotoAndPlay("close");
		}
		//----------------------------------------------------------
		public function close(){
			trace("close error panel");
			_Main.stage.removeChild(_mc);
		}
		
		//-------------------------------------------------------------------
		private function initKeyListeners()
		{
			trace("---- initKeyListeners panError");
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey_Down);
		}
		//-------------------------------------------------------------------
		private function removeKeyListeners()
		{
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKey_Down);
		}
		//-------------------------------------------------------------------
		private function onKey_Down(evt:KeyboardEvent)
		{
			
			trace("---- onKey_Down panError");
			if (evt.keyCode == Keyboard.ENTER)
			{
				startClose();
			}
		}
	}
}
